
% mtg2src_det
% Christopher J. Whalen
% University of Illinois
% 07/08/04 - Date Created
% 06/26/06 - Date Modified

% Purpose - create src and det array from mtg array.

% NB - there are some mtg files that have 'NEW' in the first line and some
% that do not. There are also some that have two columns and others that
% have 1.

% !!! replace "xyz" with "size(xyz)"
% elm - added "char(*')" to lines 39,40,50&51 ("mtg(i,1) = ")

function [src,det,mtg]=mtg2src_det(choice,file,xyz)

% fprintf('\n mtg2src_det\n');
src=cell(0);
det=cell(0);
if choice.align == 1         % Creating Dummy Source File to view HSP data
    src = cell(size(xyz,1)-3,1); % 3 b/c xyz has 3 fiducials at top
    for i=1:(size(xyz,1)-3)
        src(i)=cellstr(strcat('Pt',num2str(i)));
    end
elseif (choice.align == 2) | (choice.align == 3) 
    fid = fopen(file.mtg);
    mtg_format = fgetl(fid); % NEW header line
%     if strcmpi(mtg_format,'NEW')
        mtg = cell(10000,2); % preallocating: 1st column will be src; 2nd will be det
        mtg_line_i = fgetl(fid); % Channels in each session
        det_mtg_fmt = sscanf(mtg_line_i, '%*d %d'); % !!! was below next line
        mtg_line_i = fgetl(fid); % First mtg line with labels, channels, and wavelength
        
        if ~strcmp(det_mtg_fmt,'') % if true then 5 column mtg format
            i = 1;
            while 1
                if ~ischar(mtg_line_i), break, end
                mtg(i,1) = cellstr(char(sscanf(mtg_line_i, '%*d %s %*s %*d %*d')')); % abstracting src from line_i; * means ignore
                mtg(i,2) = cellstr(char(sscanf(mtg_line_i, '%*d %*s %s %*d %*d')')); % abstracting det from line_i

                i = i + 1;
                mtg_line_i = fgetl(fid); % 
            end
        elseif strcmp(det_mtg_fmt,'')  % if true then must be 6 column mtg format
            i = 1;
            while 1
                if ~ischar(mtg_line_i), break, end
                mtg(i,1) = cellstr(char(sscanf(mtg_line_i, '%*d %*d %s %*s %*d %*d')')); % abstracting src from line_i; * means ignore
                mtg(i,2) = cellstr(char(sscanf(mtg_line_i, '%*d %*d %*s %s %*d %*d')')); % abstracting det from line_i
                i = i + 1;
                mtg_line_i = fgetl(fid); % 
            end
        end
        
        i = i - 1;
        mtg = mtg(1:i,:);
        src = mtg(1:i,1);
        det = mtg(1:i,2);
        fclose(fid);
%     else
%         disp('mtg_format ~= ')
%         mtg_format
%         mtg = importdata(file.mtg,'\t'); % Obtain mtg array
%         if size(str2num(mtg_format),1)>0 % reads in mtg files with channels (numbers) on first line and reads over that line if present
%             start_line=2;
%         else
%             start_line=1;
%         end
%         i_src = 1;
%         i_det = 1;
%         for i=start_line:size(mtg,1)         % Pull apart sources and detectors
%             if abs(rem(i,2)-(start_line-1))==1          % src are odd numbered lines (except for mtg files with channels at the top and 1's and 2's at the bottom)
%                 src(i_src)=mtg(i);
%                 i_src = i_src + 1;
%             elseif abs(rem(i,2)-(start_line-1))==0      % det are even numbered lines
%                 det(i_det)=mtg(i);
%                 i_det = i_det + 1;
%             end
%         end
%         src=src';
%         det=det';
%     end
    
    
    
    % SRC - sort and remove repetetive rows 
	src = sort(src);
	temp_src_row = 1;
	for row=1:(size(src,1)-1)              
        if strcmp(src(row), src(row+1))==1  
        else 
          temp_src(temp_src_row,:) = src(row, :);
          temp_src_row = temp_src_row + 1;
        end
	end
	temp_src(temp_src_row,:) = src(size(src,1));
	src = temp_src;
	clear temp_src;       
    % DET - sort and remove repetetive rows 
	det = sort(det);
	temp_det_row = 1;
	for row=1:(size(det,1)-1)              
      if strcmp(det(row), det(row+1))==1  
      else 
          temp_det(temp_det_row,:) = det(row, :);
          temp_det_row = temp_det_row + 1;
      end
	end
	temp_det(temp_det_row,:) = det(size(det,1));
	det = temp_det;
	clear temp_det; 
    if size(str2num(mtg_format),1)>0  % If mtg format has channels in first line
        for k=1:2 % search src file for 1's and 2's for wavelength and remove them from src and det
            if strcmp(src(1,:),'1') | strcmp(src(1,:),'2')
                src(1)=[];
            end
            if strcmp(det(1,:),'1') | strcmp(det(1,:),'2')
                det(1)=[];
            end
        end
    end % end removal of 1's and 2's in mtg file
end

if size(src,1)==0 | size(det,1)==0
    fprintf('\nError Reading Montage File: No source or no detectors found.\nProgram may procede but no TOL will be able to be generated.\n\n');
end
if size(xyz,1) < (size(det,1)+size(src,1))
    fprintf('\nWarning: number of points in mtg file (%0.0f) is larger than number of points in the elp file (%0.0f). \n\n', (size(src,1)+size(det,1)),size(xyz,1));
end

% fprintf(' mtg2src_det done\n');



